//
//  LGPayInputView.m
//  LXCPod
//
//  Created by 刘星辰 on 2018/5/31.
//  Copyright © 2018年 刘星辰. All rights reserved.
//

#import "LGPayInputView.h"
#import "Masonry.h"

@interface LGPayInputView()
@property(nonatomic,strong)NSMutableArray <UILabel *>*labels;
@end


@implementation LGPayInputView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        [self initialUI];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
    if (self = [super initWithCoder:aDecoder])
    {
        [self initialUI];
    }
    return self;
}

- (void)initialUI
{
    self.labels = [NSMutableArray array];
    CGFloat space = (self.frame.size.width - 30 * 6)/5.0;
    for (NSInteger i = 0 ; i< 6; i++)
    {
        UILabel * label =  [UILabel new];
//        label.text = @"1";
        label.font = [UIFont systemFontOfSize:36];
        label.textColor = [UIColor blackColor];
        label.textAlignment = NSTextAlignmentCenter;
        label.numberOfLines = 0;
        [self addSubview:label];
        
        UIView *line = [UIView new];
        line.frame = CGRectMake(0, 0, 30, 1);
        line.backgroundColor = [UIColor lightGrayColor];
        [self addSubview:line];
        
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.offset(0);
            make.left.equalTo(self.mas_left).offset((30 + space) * i);
            make.width.offset(30);
        }];
        
        [line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.offset((30 + space) * i);
            make.size.mas_equalTo(line.size);
            make.bottom.offset(1);
        }];
        
        [self.labels addObject:label];
    }
    
    
}

- (BOOL)hasText
{
    return self.password.length > 0;
}


/**
 *  插入文本
 */
- (void)insertText:(NSString *)text
{
    if (self.password.length < 6)
    {
        //判断是否是数字
        NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:@"0123456789"] invertedSet];
        NSString*filtered = [[text componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
        BOOL basicTest = [text isEqualToString:filtered];
        if(basicTest)
        {
            UILabel *label = self.labels[self.password.length];
            label.text = text;
            self.password = [NSString stringWithFormat:@"%@%@",self.password,text];
            if (self.password.length == 6)
            {
                if (self.inputEndAction)
                {
                    self.inputEndAction(self);
                }
            }
        }
    }
}

/**
 *  删除文本
 */
- (void)deleteBackward
{
    if (self.password.length > 0)
    {
        UILabel *label = [self.labels objectAtIndex:self.password.length - 1];
        label.text = @"";
        self.password = [self.password substringToIndex:self.password.length - 1];
    }
    [self setNeedsDisplay];
}

- (NSString *)password
{
    if (_password == nil)
    {
        _password = @"";
    }
    return _password;
}

/**
 *  是否能成为第一响应者
 */
- (BOOL)canBecomeFirstResponder
{
    return YES;
}

/**
 *  点击成为第一相应者
 */
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    if (![self isFirstResponder]) {
        [self becomeFirstResponder];
    }
}

- (UIKeyboardType)keyboardType
{
    return UIKeyboardTypeNumberPad;
}



@end

